package org.admanager.piadl.plugin.diagram.navigator;

import org.admanager.piadl.plugin.diagram.edit.parts.AcmeRootEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ArchitectureEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ArchitectureNameEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.Component2EditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ComponentEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ComponentName2EditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ComponentNameEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.Connector2EditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ConnectorEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ConnectorName2EditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ConnectorNameEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.Port2EditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.PortEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.RelayEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.UnificationEditPart;
import org.admanager.piadl.plugin.diagram.part.PiADLADManagementMetamodelDiagramEditorPlugin;
import org.admanager.piadl.plugin.diagram.part.PiADLADManagementMetamodelVisualIDRegistry;
import org.admanager.piadl.plugin.diagram.providers.PiADLADManagementMetamodelElementTypes;
import org.admanager.piadl.plugin.diagram.providers.PiADLADManagementMetamodelParserProvider;
import org.admanager.piadladmanagement.plugin.piadladmanagement.Architecture;
import org.admanager.piadladmanagement.plugin.piadladmanagement.Port;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

/**
 * @generated
 */
public class PiADLADManagementMetamodelNavigatorLabelProvider extends
		LabelProvider implements ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		PiADLADManagementMetamodelDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		PiADLADManagementMetamodelDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof PiADLADManagementMetamodelNavigatorItem
				&& !isOwnView(((PiADLADManagementMetamodelNavigatorItem) element)
						.getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof PiADLADManagementMetamodelNavigatorGroup) {
			PiADLADManagementMetamodelNavigatorGroup group = (PiADLADManagementMetamodelNavigatorGroup) element;
			return PiADLADManagementMetamodelDiagramEditorPlugin.getInstance()
					.getBundledImage(group.getIcon());
		}

		if (element instanceof PiADLADManagementMetamodelNavigatorItem) {
			PiADLADManagementMetamodelNavigatorItem navigatorItem = (PiADLADManagementMetamodelNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (PiADLADManagementMetamodelVisualIDRegistry.getVisualID(view)) {
		case Port2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://piadladmanagement/1.0?Port", PiADLADManagementMetamodelElementTypes.Port_3010); //$NON-NLS-1$
		case ArchitectureEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://piadladmanagement/1.0?Architecture", PiADLADManagementMetamodelElementTypes.Architecture_3008); //$NON-NLS-1$
		case RelayEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://piadladmanagement/1.0?Relay", PiADLADManagementMetamodelElementTypes.Relay_4006); //$NON-NLS-1$
		case ConnectorEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://piadladmanagement/1.0?Connector", PiADLADManagementMetamodelElementTypes.Connector_2003); //$NON-NLS-1$
		case Connector2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://piadladmanagement/1.0?Connector", PiADLADManagementMetamodelElementTypes.Connector_3011); //$NON-NLS-1$
		case PortEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://piadladmanagement/1.0?Port", PiADLADManagementMetamodelElementTypes.Port_3007); //$NON-NLS-1$
		case AcmeRootEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?http://piadladmanagement/1.0?Architecture", PiADLADManagementMetamodelElementTypes.Architecture_1000); //$NON-NLS-1$
		case Component2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://piadladmanagement/1.0?Component", PiADLADManagementMetamodelElementTypes.Component_3009); //$NON-NLS-1$
		case UnificationEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://piadladmanagement/1.0?Unification", PiADLADManagementMetamodelElementTypes.Unification_4005); //$NON-NLS-1$
		case ComponentEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://piadladmanagement/1.0?Component", PiADLADManagementMetamodelElementTypes.Component_2004); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = PiADLADManagementMetamodelDiagramEditorPlugin
				.getInstance().getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null
				&& elementType != null
				&& PiADLADManagementMetamodelElementTypes
						.isKnownElementType(elementType)) {
			image = PiADLADManagementMetamodelElementTypes
					.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof PiADLADManagementMetamodelNavigatorGroup) {
			PiADLADManagementMetamodelNavigatorGroup group = (PiADLADManagementMetamodelNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof PiADLADManagementMetamodelNavigatorItem) {
			PiADLADManagementMetamodelNavigatorItem navigatorItem = (PiADLADManagementMetamodelNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (PiADLADManagementMetamodelVisualIDRegistry.getVisualID(view)) {
		case Port2EditPart.VISUAL_ID:
			return getPort_3010Text(view);
		case ArchitectureEditPart.VISUAL_ID:
			return getArchitecture_3008Text(view);
		case RelayEditPart.VISUAL_ID:
			return getRelay_4006Text(view);
		case ConnectorEditPart.VISUAL_ID:
			return getConnector_2003Text(view);
		case Connector2EditPart.VISUAL_ID:
			return getConnector_3011Text(view);
		case PortEditPart.VISUAL_ID:
			return getPort_3007Text(view);
		case AcmeRootEditPart.VISUAL_ID:
			return getArchitecture_1000Text(view);
		case Component2EditPart.VISUAL_ID:
			return getComponent_3009Text(view);
		case UnificationEditPart.VISUAL_ID:
			return getUnification_4005Text(view);
		case ComponentEditPart.VISUAL_ID:
			return getComponent_2004Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getPort_3010Text(View view) {
		Port domainModelElement = (Port) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin
					.getInstance()
					.logError(
							"No domain element for view with visualID = " + 3010); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getArchitecture_3008Text(View view) {
		IParser parser = PiADLADManagementMetamodelParserProvider.getParser(
				PiADLADManagementMetamodelElementTypes.Architecture_3008, view
						.getElement() != null ? view.getElement() : view,
				PiADLADManagementMetamodelVisualIDRegistry
						.getType(ArchitectureNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5009); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getRelay_4006Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getConnector_2003Text(View view) {
		IParser parser = PiADLADManagementMetamodelParserProvider.getParser(
				PiADLADManagementMetamodelElementTypes.Connector_2003, view
						.getElement() != null ? view.getElement() : view,
				PiADLADManagementMetamodelVisualIDRegistry
						.getType(ConnectorNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5010); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getConnector_3011Text(View view) {
		IParser parser = PiADLADManagementMetamodelParserProvider.getParser(
				PiADLADManagementMetamodelElementTypes.Connector_3011, view
						.getElement() != null ? view.getElement() : view,
				PiADLADManagementMetamodelVisualIDRegistry
						.getType(ConnectorName2EditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5008); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPort_3007Text(View view) {
		Port domainModelElement = (Port) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin
					.getInstance()
					.logError(
							"No domain element for view with visualID = " + 3007); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getArchitecture_1000Text(View view) {
		Architecture domainModelElement = (Architecture) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin
					.getInstance()
					.logError(
							"No domain element for view with visualID = " + 1000); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getComponent_3009Text(View view) {
		IParser parser = PiADLADManagementMetamodelParserProvider.getParser(
				PiADLADManagementMetamodelElementTypes.Component_3009, view
						.getElement() != null ? view.getElement() : view,
				PiADLADManagementMetamodelVisualIDRegistry
						.getType(ComponentName2EditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5007); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getUnification_4005Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getComponent_2004Text(View view) {
		IParser parser = PiADLADManagementMetamodelParserProvider.getParser(
				PiADLADManagementMetamodelElementTypes.Component_2004, view
						.getElement() != null ? view.getElement() : view,
				PiADLADManagementMetamodelVisualIDRegistry
						.getType(ComponentNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5011); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return AcmeRootEditPart.MODEL_ID
				.equals(PiADLADManagementMetamodelVisualIDRegistry
						.getModelID(view));
	}

}
